---@class components
---@field webbert_skitter_misc component_webbert_skitter_misc # 这个组件用于elf的一些行为

-- local function on_val(self, value)
    -- self.inst.replica.webbert_skitter_misc:SetVal(value)
-- end

---@class component_webbert_skitter_misc
---@field inst ent
---@field hobby integer # 性格分区序号
---@field hobby_id integer # 随机挑选id序号
---@field when_spawn_maxhp number|nil # 创建时 最大生命值
---@field when_spawn_hp_recovery number|nil # 创建时 回复生命值
---@field when_spawn_hp_recovery_player number|nil # 创建时 为玩家回复量
---@field _temp_master_userid string|nil # 临时主人 userid
---@field _exchange_head ent|nil # 头部更换的特效实体
---@field preparedfood_prefabid string|nil # 头部是不是override的,不是则用实体
---@field head_bank string|nil # 
---@field head_build string|nil
---@field head_anim string|nil
---@field stop_thinking number|nil # mod: 线程思考 <br> 为`number`时, 停止思考, 并且每次重新开始的时间间隔为这个数字, 因此要`>0` <br> 为`nil`时保持思考
---@field _grass ent|nil # 当前正在帮忙干小活的目标
---@field _helps_key string|nil # 工作表里的key
---@field _tree ent|nil # 砍树的目标
---@field _mine ent|nil
---@field _fx_sleep_zzz ent|nil # 睡眠的粒子特效zzz
---@field _fx_heal_whenfree ent|nil # 治愈射线的粒子特效
---@field _task_tree number|nil # 任务: 砍树持续时间
---@field _task_mine number|nil # 任务: 采矿持续时间
---@field _task_incombat boolean|nil # 在战斗
local webbert_skitter_misc = Class(
---@param self component_webbert_skitter_misc
---@param inst ent
function(self, inst)
    self.inst = inst
    -- self.val = 0
    self._temp_master_userid = nil

    self.head_bank = nil
    self.head_build = nil
    self.head_anim = nil

    self.when_spawn_maxhp = 20
    self.when_spawn_hp_recovery = 0
    self.when_spawn_hp_recovery_player = 1

    self.preparedfood_prefabid = nil

    self.hobby = 1
    self.hobby_id = 1
end,
nil,
{
    -- val = on_val,
    hobby = function (self,val)
        self.inst.replica.webbert_skitter_misc.hobby:set(val)
    end,
    hobby_id = function (self,val)
        self.inst.replica.webbert_skitter_misc.hobby_id:set(val)
    end,
    -- when_spawn_maxhp = function (self,val)
    --     self.inst.replica.webbert_skitter_misc.when_spawn_maxhp:set(val)
    -- end,
    -- when_spawn_hp_recovery = function (self,val)
    --     self.inst.replica.webbert_skitter_misc.when_spawn_hp_recovery:set(val)
    -- end,
    -- when_spawn_hp_recovery_player = function (self,val)
    --     self.inst.replica.webbert_skitter_misc.when_spawn_hp_recovery_player:set(val)
    -- end,
})

function webbert_skitter_misc:OnSave()
    local data = {
        -- val = self.val
        _temp_master_userid = self._temp_master_userid,
        head_bank = self.head_bank,
        head_build = self.head_build,
        head_anim = self.head_anim,
        when_spawn_maxhp = self.when_spawn_maxhp,
        when_spawn_hp_recovery = self.when_spawn_hp_recovery,
        when_spawn_hp_recovery_player = self.when_spawn_hp_recovery_player,
        preparedfood_prefabid = self.preparedfood_prefabid,
        hobby = self.hobby,
        hobby_id = self.hobby_id,
    }

    local percent = self.inst.components.health:GetPercent()
    data.hp_percent = percent

    return data
end

function webbert_skitter_misc:OnLoad(data)
    -- self.val = data.val or 0
    self._temp_master_userid = data._temp_master_userid or nil
    self.head_bank = data.head_bank or nil
    self.head_build = data.head_build or nil
    self.head_anim = data.head_anim or nil
    self.when_spawn_maxhp = data.when_spawn_maxhp or 20
    self.when_spawn_hp_recovery = data.when_spawn_hp_recovery or 0
    self.when_spawn_hp_recovery_player = data.when_spawn_hp_recovery_player or 1
    self.preparedfood_prefabid = data.preparedfood_prefabid or nil
    self.hobby = data.hobby or 1
    self.hobby_id = data.hobby_id or 1

    self:ExchangeHead(self.preparedfood_prefabid,self.head_bank,self.head_build,self.head_anim)

    if self.inst.components.health then
        self.inst.components.health.maxhealth = self.when_spawn_maxhp
        if data.hp_percent and not self.inst.components.health:IsDead() then
            self.inst.components.health:SetPercent(data.hp_percent)
        end
    end

end

---强迫停止思考
---@param stop_thinking number # 线程思考的时间间隔
function webbert_skitter_misc:StopThinking(stop_thinking)
    self._grass = nil
    self.stop_thinking = stop_thinking
end

---开始思考
function webbert_skitter_misc:StartThinking()
    self.stop_thinking = nil
end

---设置临时主人,必须是玩家
function webbert_skitter_misc:SetTempMaster(player)
    if player:HasTag('player') and player.userid then
        self._temp_master_userid = player.userid
    end
end

---获取临时主人
---@return ent|nil
function webbert_skitter_misc:GetTempMaster()
    return self._temp_master_userid and UserToPlayer(self._temp_master_userid) or nil
end

---是否有临时主人
function webbert_skitter_misc:HasTempMaster()
    return self._temp_master_userid ~= nil -- 这样就好,因为即使玩家下线了,他依旧在等待他的临时主人上线
end

---替换头部
---@param preparedfood_prefabid string # 通道名
---@param bank string
---@param build string
---@param anim string
function webbert_skitter_misc:ExchangeHead(preparedfood_prefabid, bank,build,anim)
    self.head_bank = bank
    self.head_build = build
    self.head_anim = anim

    if self._exchange_head and self._exchange_head:IsValid() then
        self._exchange_head:Remove()
    end
    self._exchange_head = nil
    if preparedfood_prefabid then
        self.preparedfood_prefabid = preparedfood_prefabid
        self.inst.AnimState:ShowSymbol("red_art")
        self.inst.AnimState:OverrideSymbol('red_art',build,preparedfood_prefabid)
    else
        self.inst.AnimState:HideSymbol("red_art")
        self._exchange_head = SpawnPrefab('fx_webbert_skitter_head')
        self._exchange_head.AnimState:SetBank(bank)
        self._exchange_head.AnimState:SetBuild(build)
        self._exchange_head.AnimState:PlayAnimation(anim,true)

        self._exchange_head.entity:AddFollower()
        self._exchange_head.entity:SetParent(self.inst.entity)
        self._exchange_head.Follower:FollowSymbol(self.inst.GUID, "red_art", 0, 0, 0)
    end
end

---仅生成时调用
---@param when_spawn_maxhp number
---@param when_spawn_hp_recovery number
---@param when_spawn_hp_recovery_player number
function webbert_skitter_misc:Set3SizeWhenSpawn(when_spawn_maxhp,when_spawn_hp_recovery,when_spawn_hp_recovery_player)
    self.when_spawn_maxhp = when_spawn_maxhp
    self.when_spawn_hp_recovery = when_spawn_hp_recovery
    self.when_spawn_hp_recovery_player = when_spawn_hp_recovery_player

    self.inst.components.health.maxhealth = when_spawn_maxhp
    self.inst.components.health:SetPercent(1)

    -- 随机性格
    local hobbys = STRINGS.MOD_WEBBER_THE_TRAINER.elf.hobby
    self.hobby = math.random(1,#hobbys)
    self.hobby_id = math.random(1,#hobbys[self.hobby].id)

    local name = STRINGS.MOD_WEBBER_THE_TRAINER.elf.hobby[self.hobby].id[self.hobby_id]
    if self.inst.components.named then
        self.inst.components.named:SetName(name)
    end
end

return webbert_skitter_misc